<?php

class Product extends CI_Controller
{
    function __construct()
    {
        parent ::__construct();
        session_start();
        $this->load->model('category_model');
        $this->load->model('product_model');
        $this->load->helper('main_helper');
        //$this->output->enable_profiler();//dung de debug
    }

    function index()
    {
        $this->load->library('pagination');

        $config['base_url'] = base_url() . 'product/index/'; //xac dinh trang can phan trang
        $config['total_rows'] = $this->product_model->countRows(); //tong so record
        $config['per_page'] = 9; //xac dinh so record o moi trang
        $config['num_links'] = 20;
        $config['uri_segment'] = 3; //xac dinh segment chua page number
        $config['full_tag_open'] = '<div id="page">'; //tao the div cho link phan trang
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);

        $data['products'] = $this->product_model->getAllByStatus(null, $this->uri->segment(3), $config['per_page']);
        $data['categories'] = $this->category_model->getAll();
        $this->load->view('fontend/header', $data);
        $this->load->view('fontend/mainleft', $data);
        $this->load->view('fontend/product/product_view', $data);
        $this->load->view('fontend/footer', $data);
    }
    function category($category_url)
    {

        $data['current_category'] = $this->category_model->getByUrl($category_url);
        $data['categories'] = $this->category_model->getAll($data['current_category']->category_id);
        if (empty($data['current_category'])) {
            show_404();
        }
        $this->load->library('pagination');

        $config['base_url'] = base_url() . $data['current_category']->category_url; //xac dinh trang can phan trang
        $config['total_rows'] = $this->product_model->countRows($data['current_category']->category_id); //tong so record
        $config['per_page'] = 9; //xac dinh so record o moi trang
        $config['num_links'] = 20;
        $config['uri_segment'] = 2; //xac dinh segment chua page number
        $config['full_tag_open'] = '<div id="page">'; //tao the div cho link phan trang
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);

        $data['products'] = $this->product_model->getAllByStatus($data['current_category']->category_id, $this->uri->segment(2), $config['per_page']);

        $this->load->view('fontend/header', $data);
        $this->load->view('fontend/mainleft', $data);
        $this->load->view('fontend/product/product_view', $data);
        $this->load->view('fontend/footer', $data);
    }

    function show_product($product_url)
    {
        $data['current_product'] = $this->product_model->getByProductUrl($product_url);
        $data['categories'] = $this->category_model->getAll();

        $this->load->view('fontend/header', $data);
        $this->load->view('fontend/mainleft', $data);
        $this->load->view('fontend/product/show_product', $data);
        $this->load->view('fontend/footer', $data);
    }

    function search()
    {
        $data['categories'] = $this->category_model->getAll();
        $keyword = $this->input->get('search');
        $data['products'] = $this->product_model->search($keyword);

        $this->load->view('fontend/header', $data);
        $this->load->view('fontend/mainleft', $data);
        $this->load->view('fontend/product/product_search', $data);
        $this->load->view('fontend/footer', $data);
    }

    function search_by_price()
    {
        $data['categories'] = $this->category_model->getAll();
        $from = $this->input->get('from');
        $to = $this->input->get('to');
        $data['products'] = $this->product_model->searchByPrice($from, $to);

        $this->load->view('fontend/header', $data);
        $this->load->view('fontend/mainleft', $data);
        $this->load->view('fontend/product/product_search', $data);
        $this->load->view('fontend/footer', $data);
    }
}

?>
